#include<string>
#include<iostream>
#include<vector>
#include <ostream>
#include <fstream>
#include "../source/server.hpp"
#include "../source/http/http.hpp"


size_t split(const std::string str , const std::string sep , std::vector<std::string>* arr)
{
  size_t offset = 0;
  while(offset < str.size())
  {
    size_t pos = str.find(sep,offset);
    if(pos == std::string::npos)
    {
      //没有找到，如果是在结尾，直接返回
      if(pos == str.size()) break; 
      //把末尾字符加上
      arr->push_back(str.substr(offset));
      return arr->size();
    }
    //找到了，从 offset ~ pos - offset 
    if(pos != offset) arr->push_back(str.substr(offset,pos-offset));
    offset = pos + sep.size();
  }
  return arr->size();
}

    static bool ReadFile(const std::string filename,std::string* buf)   
    {
       std::ifstream ifs(filename,std::ios::binary); 
       if(!ifs.is_open())
       {
         ELOG("Open %s Failed!",filename.c_str());
         return false; 
       }
       size_t fsize = 0; 
       ifs.seekg(0,ifs.end);//跳转到文件末尾 
       fsize = ifs.tellg(); //获取当前位置到初始位置的大小 
       ifs.seekg(ifs.beg) ; //回到初始位置 
       buf->resize(fsize);
       ifs.read(&(*buf)[0],fsize); //把文件读到字符串str   
       if(!ifs.good())
       {
         ELOG("READ %s FAILED",filename.c_str());
         ifs.close();
         return false;
       }
       ifs.close();
       return true; 
    }

bool WriteFile(const std::string& filename,const std::string& buf)
{
  //打开文件，没有则创建
  std::ofstream os(filename,std::ios::binary | std::ios::trunc); 
  if(!os.is_open())
  {
    ELOG("FILE %s OPEN FAILED",filename.c_str());
    return false; 
  }
  os.write(buf.c_str(),buf.size());
  if(!os.good())
  {
    ELOG("FILE %s WRITE FAILED!",filename.c_str());
    os.close();
    return false; 
  }
  os.close();
  return true;
}


static std::string UrlEcode(const std::string& url , bool convert_space_to_plus)
{
  std::string res ; 
  for(auto& c : url)
  {
    if(c == '.' || c == '-' || c == '~' || c == '_' || isalnum(c))
    {
      res += c; 
      continue; 
    }
    if(c == ' ' && convert_space_to_plus)
    {
      res += '+';
      continue; 
    }
    //编码 
    char tmp[4] ;
    snprintf(tmp,4,"%%%02X",c);
    res += tmp; 
  }
  return res;
}




int main()
{
//  std::string str = ",,,,,,,,,,,,,abc";
//  std::vector<std::string> v;
//  split(str,",",&v);
//  for(auto& s : v)
//    std::cout << s << std::endl;
//
//    std::string buf;
//    ReadFile("./server.cc",&buf);
//    WriteFile("tttttttt.c",buf);
  //std::string url = "C++";
  //std::string res = UrlEcode(url,false);
  //std::cout << res<< "]" << std::endl;
  //std::string tmp = wyl::Util::UrlDcode(res,false);
  //std::cout << tmp<< "]" << std::endl;
  
  //std::string desc = wyl::Util::StatuDesc(404);
  //std::cout << "[" << desc << "]" << std::endl;
  //desc = wyl::Util::StatuDesc(200);
  //std::cout << "[" << desc << "]" << std::endl;

  //std::cout << "----------------------------------" << std::endl;
  //std::string mime = wyl::Util::ExtMine("aaaaa.txt") ; 
  //std::cout << "[" << mime << "]" << std::endl;
  //mime = wyl::Util::ExtMine("aaaaa.png") ; 
  //std::cout << "[" << mime << "]" << std::endl;
  //mime = wyl::Util::ExtMine("aaaaa.xxxxxxxxxxxx") ; 
  //std::cout << "[" << mime << "]" << std::endl;

  //std::cout << wyl::Util::IsDirectory("server.cc")<<std::endl;
  //std::cout << wyl::Util::IsDirectory("testdir")<<std::endl;
  //std::cout << wyl::Util::IsRegular("server.cc")<<std::endl;
  //std::cout << wyl::Util::IsRegular("test.dir")<<std::endl;

  std::cout << wyl::Util::ValidPath("/index.html") << std::endl; 
  std::cout << wyl::Util::ValidPath("/index.html/../index.html") << std::endl; 
  std::cout << wyl::Util::ValidPath("/../index.html") << std::endl; 
  std::cout << wyl::Util::ValidPath("/index.html/../../index.html") << std::endl; 
  return 0 ;
}
